package com.example.demo.controller;


import com.example.demo.model.SysRole;
import com.example.demo.responsehandler.ResponseUtil;
import com.example.demo.service.ISysRoleAclService;
import com.example.demo.service.ISysRoleService;
import com.example.demo.util.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.demo.codegenerator.BaseController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Webgasd
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/demo/sys-role")
@Api(tags = {"角色管理控制器"})
public class SysRoleController extends BaseController {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleAclService sysRoleAclService;

    @ApiOperation(value = "获取全部角色列表", notes = "需要分页前端header传入range参数，不需传入range即获取整个list")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "APP_KEY", value = "拦截器验证K", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "SECRET", value = "拦截器验证V", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "Range", value = "分页", dataType = "String", defaultValue = "items=0-9", example = "items=0-9", required = true, paramType = "header"),
            @ApiImplicitParam(name = "remark", value = "角色所属系统", dataType = "String", defaultValue = "智慧市场", example = "智慧市场", required = true, paramType = "query")})
    @GetMapping("/getRoleList")
    public Map<String, Object> getRoleList(@RequestParam("remark") String remark) {
        return ResponseUtil.toSuccessMessage("", sysRoleService.getRoleList(remark));
    }

    @ApiOperation(value = "新增一个角色")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "APP_KEY", value = "拦截器验证K", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "SECRET", value = "拦截器验证V", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "Range", value = "分页", dataType = "String", defaultValue = "items=0-9", example = "items=0-9", required = true, paramType = "header")})
    @PostMapping("/saveRole")
    public void saveRole(@RequestBody SysRole sysRole) throws IOException {
        ResponseUtil.showMessage("", "", sysRoleService.saveRole(sysRole));
    }

    @ApiOperation(value = "修改角色信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "APP_KEY", value = "拦截器验证K", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "SECRET", value = "拦截器验证V", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "Range", value = "分页", dataType = "String", defaultValue = "items=0-9", example = "items=0-9", required = true, paramType = "header")})
    @PutMapping("/updateRole")
    public void updateRole(@RequestBody SysRole sysRole) throws IOException {
        ResponseUtil.showMessage("", "", sysRoleService.updateRole(sysRole));
    }

    @ApiOperation(value = "根据主键id删除某个角色")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "APP_KEY", value = "拦截器验证K", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "SECRET", value = "拦截器验证V", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "Range", value = "分页", dataType = "String", defaultValue = "items=0-9", example = "items=0-9", required = true, paramType = "header"),
            @ApiImplicitParam(name = "id", value = "角色主键", dataType = "int", defaultValue = "0", example = "0", required = true, paramType = "query")})
    @DeleteMapping("/deleteRoleById")
    public void deleteRoleById(@RequestParam("id") Integer id) throws IOException {
        ResponseUtil.showMessage("", "", sysRoleService.deleteRoleById(id));
    }

    @ApiOperation(value = "修改某一角色的权限")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "APP_KEY", value = "拦截器验证K", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "SECRET", value = "拦截器验证V", dataType = "String", defaultValue = "111", example = "111", required = true, paramType = "header"),
            @ApiImplicitParam(name = "Range", value = "分页", dataType = "String", defaultValue = "items=0-9", example = "items=0-9", required = true, paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "角色主键", dataType = "int", defaultValue = "0", example = "0", required = true, paramType = "query"),
            @ApiImplicitParam(name = "aclIds", value = "模块列表字符串", dataType = "String", defaultValue = "0,1,2", example = "0,1,2", required = false, paramType = "query")})
    @GetMapping("/changeRoleAcl")
    public void changeRoleAcl(@RequestParam("roleId") Integer roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) throws IOException {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        ResponseUtil.showMessage("", "", sysRoleAclService.changeRoleAcl(roleId, aclIdList));
    }
}
